/**
 * Created by xuxueli on 17/4/24.
 */

$(function () {
    $.ajaxSetup({
        complete: function (XMLHttpRequest, textStatus) {
            var url = XMLHttpRequest.getResponseHeader("redirectUrl");
            if (url) {
                window.location.href = url;
            }
        }
    });
    //多个邮箱验证
    jQuery.validator.addMethod("isEmails", function (value, element) {
        var tel = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        var isOk = true;
        if (value == '') {
            isOk = true;
        } else {
            var emails = value.split(",");
            for (var i = 0; i < emails.length; i++) {
                if (!(tel.test(emails[i]))) {
                    isOk = false;
                }
            }
        }
        return this.optional(element) || isOk;
    }, "邮箱格式不正确");

    $("#addJobInfo").validate({
        rules: {
            jobName: {
                required: true,
                maxlength: 32
            },
            jobGroup: {
                required: true,
                maxlength: 32
            },
            cronExpression: {
                required: true,
                maxlength: 32
            },
            jobDesc: {
                maxlength: 32
            },
            service: {
                required: true,
                maxlength: 64
            },
            method: {
                required: true,
                maxlength: 64
            },
            alarmEmail: {
                maxlength: 250,
                isEmails: true
            },
            concurrentCount: {
                required: {
                    depends: function (element) {
                        return $("#concurrentNum").val() == '1';
                    }
                },
                digits: true
            },
            parameters: {
                maxlength: 200
            },scriptPath: {
                required: true,
                maxlength: 255
            }
        },
        messages: {
            jobName: {
                required: "请输入任务名",
                maxlength: "最多32个字符"
            },
            jobGroup: {
                required: "请输入组名",
                maxlength: "最多32个字符"
            },
            cronExpression: {
                required: "触发规则是必须的",
                maxlength: "最多32个字符"
            },
            jobDesc: {
                maxlength: "最多32个字符"
            },
            method: {
                required: "请输入method",
                maxlength: "最多64个字符"
            }, service: {
                required: "请输入service",
                maxlength: "最多64个字符"
            },
            parameters: {
                maxlength: "最多200个字符"
            }, alarmEmail: {
                maxlength: "最多250个字符",
                isEmails: "邮箱格式不正确"
            },
            concurrentCount: {
                required: "当数量有限时，必须输入数量",
                digits: "请输入整数"
            },
            scriptPath: {
                required: "请输入脚本路径",
                maxlength: "最多255个字符"
            }
        }
    });

    $("#updateJobInfo").validate({
        rules: {
            jobName: {
                required: true,
                maxlength: 32
            },
            jobGroup: {
                required: true,
                maxlength: 32
            },
            cronExpression: {
                required: true,
                maxlength: 32
            },
            jobDesc: {
                maxlength: 32
            },
            service: {
                required: true,
                maxlength: 64
            },
            scriptPath: {
                required: true,
                maxlength: 255
            },
            method: {
                required: true,
                maxlength: 64
            },
            alarmEmail: {
                maxlength: 250,
                isEmails: true
            },
            concurrentCount: {
                required: {
                    depends: function (element) {
                        return $("#concurrentNum").val() == '1';
                    }
                },
                digits: true
            },
            parameters: {
                maxlength: 200
            }
        },
        messages: {
            jobName: {
                required: "请输入任务名",
                maxlength: "最多32个字符"
            },
            jobGroup: {
                required: "请输入组名",
                maxlength: "最多32个字符"
            },
            cronExpression: {
                required: "触发规则是必须的",
                maxlength: "最多32个字符"
            },
            jobDesc: {
                maxlength: "最多32个字符"
            },
            method: {
                required: "请输入method",
                maxlength: "最多64个字符"
            }, service: {
                required: "请输入service",
                maxlength: "最多64个字符"
            },
            parameters: {
                maxlength: "最多200个字符"
            }, alarmEmail: {
                maxlength: "最多250个字符",
                isEmails: "邮箱格式不正确"
            },
            concurrentCount: {
                required: "当数量有限时，必须输入数量",
                digits: "请输入整数"
            },
            scriptPath: {
                required: "请输入脚本路径",
                maxlength: "最多255个字符"
            }
        }
    });
});

function loadDiv(url) {
    $.ajax({
        url: url,
        dataType: "html",
        success: function (data) {
            $(".content").html(data);
        }
    });
}

function executeJob(jobId) {
    if (jobId == '') {
        noticeMsg("jobId不能为空", 1);
        return;
    }
    $.ajax({
        url: "/jobInfo/executeJob",
        data: {"jobId": jobId},
        dataType: "json",
        success: function (data) {
            if ("0000" == data.returnCode) {
                noticeMsg("执行成功", 0, 1);
            } else {
                noticeMsg(data.returnMsg, 1);
            }
        }
    });
}


function selectNum(obj) {
    var formId = $(obj).closest("form").attr("id");
    var value = $("#" + formId + " #concurrentNum").val();
    if (value == '1') {
        $("#" + formId + " #concurrentCount").show();
    } else {
        $("#" + formId + " #concurrentCount").hide();
    }
}

/**
 * 脚本类型设置
 * @param obj
 */
function putPath(obj) {
    var formId = $(obj).closest("form").attr("id");
    var value = $("#" + formId + " #taskType").val();
    if (value == '0') {
        $("#" + formId + " #scriptPath").hide();
    } else {
        $("#" + formId + " #scriptPath").show();
    }
}


function saveJobInfo() {
    if ($("#addJobInfo").valid()) {
        $.ajax({
            url: $("#addJobInfo").attr("action"),
            data: $("#addJobInfo").serialize(),
            dataType: "json",
            success: function (data) {
                if (data.returnCode == '0000') {
                    noticeMsg("成功", 0, 1);
                } else {
                    noticeMsg(data.returnMsg == null ? data.errorMsg : data.returnMsg, 1);
                }
            },
            error: function () {
                noticeMsg("发生错误", 1);
            }
        });
    }
}

function updateJobInfo() {
    if ($("#updateJobInfo").valid()) {
        $.ajax({
            url: $("#updateJobInfo").attr("action"),
            data: $("#updateJobInfo").serialize(),
            dataType: "json",
            success: function (data) {
                if (data.returnCode == '0000') {
                    noticeMsg("成功", 0, 1);
                } else {
                    noticeMsg(data.returnMsg == null ? data.errorMsg : data.returnMsg, 1);
                }
            },
            error: function () {
                noticeMsg("发生错误", 1);
            }
        });
    }
}

function pauseJob(jobId) {
    if (jobId == '') {
        noticeMsg("jobId不能为空", 1);
        return;
    }
    $.ajax({
        url: "/jobInfo/pauseJob",
        data: {"jobId": jobId},
        dataType: "json",
        success: function (data) {
            if ("0000" == data.returnCode) {
                noticeMsg("成功", 0, 1);
            } else {
                noticeMsg(data.returnMsg == null ? data.errorMsg : data.returnMsg, 1);
            }
        }
    });
}

function resumeJob(jobId) {
    if (jobId == '') {
        noticeMsg("jobId不能为空", 1);
        return;
    }
    $.ajax({
        url: "/jobInfo/resumeJob",
        data: {"jobId": jobId},
        dataType: "json",
        success: function (data) {
            if ("0000" == data.returnCode) {
                noticeMsg("执行成功", 0, 1);
            } else {
                noticeMsg(data.returnMsg == null ? data.errorMsg : data.returnMsg, 1);
            }
        }
    });
}

function deleteJob(jobId) {
    if (jobId == '') {
        noticeMsg("jobId不能为空", 1);
        return;
    }
    if (confirm("确定删除这条记录")) {
        $.ajax({
            url: "/jobInfo/deleteJob",
            data: {"jobId": jobId},
            dataType: "json",
            success: function (data) {
                if ("0000" == data.returnCode) {
                    noticeMsg("执行成功", 0, 1);
                } else {
                    noticeMsg(data.returnMsg, 1);
                }
            }
        });
    }
}

function editJob(jobId) {
    if (jobId == '') {
        noticeMsg("jobId不能为空", 1);
        return;
    }
    $.ajax({
        url: "/jobInfo/editJob",
        data: {"jobId": jobId},
        dataType: "json",
        success: function (data) {
            if ("0000" == data.returnCode) {
                //开始替换
                $("#updateModal").modal("show");
                for (var key in data.job) {
                    $("#updateJobInfo :input[name*=" + key + "]").val(data.job[key]);
                }
                //特殊处理
                var num = data.job["concurrentNum"];
                if (num == 0) {
                    $("#updateJobInfo #concurrentNum").val(num);
                    $("#updateJobInfo #concurrentCount").hide();
                } else {
                    $("#updateJobInfo #concurrentNum").val(1);
                    $("#updateJobInfo #concurrentCount").val(num);
                    $("#updateJobInfo #concurrentCount").show();
                }
                var num = data.job["taskType"];
                $("#updateJobInfo #taskType").val(num);
                if (num == 0) {
                    $("#updateJobInfo #scriptPath").hide();
                } else {
                    $("#updateJobInfo #scriptPath").show();
                }
            } else {
                noticeMsg(data.returnMsg == null ? data.errorMsg : data.returnMsg, 1);
            }
        }
    });
}